<?php
/**
 * GetAliases
 * The MODx snippet for get the alias.
 *
 * This project page:
 * https://code.google.com/p/modx-getaliases/
 *
 * Licensed under the GPL Version 2 licenses:
 * http://www.gnu.org/licenses/gpl.html
 *
 * Version: 1.0.2
 * Author: Kaoru Ishikura
 * Date: Fri Feb 24 13:43:10 2010 -0900
 */

$settings = array(
    /**
     * $startId [ integer ]
     * You can determine resource ID of start. Current resource by default.
     */
    'startId'          => isset($startId) ? (integer)$startId : $modx->documentIdentifier,

    /**
     * $number [ integer ]
     * You can determine the number to get an alias. NULL by default.
     */
	'number'           => isset($number) ? (integer)$number : NULL,

    /**
     * $level [ integer ]
     * You can determine the level of site structure. NULL by default.
     */
	'level'            => isset($level) ? (integer)$level : NULL,

    /**
     * $sortOrderAsc [ 0 | 1 ]
     * You can determine the sort order. On by default.
     */
    'sortOrderAsc'     => isset($sortOrderAsc) ? (integer)$sortOrderAsc : 1,

    /**
     * $respectHidemenu [ 0 | 1 ]
     * Setting this to 1 will respect the hidemenu setting of the resource
     * and not include it in aliases. Off by default.
     */
    'respectHidemenu'  => isset($respectHidemenu) ? (integer)$respectHidemenu : 0,

    /**
     * $showCurrentAlias [ 0 | 1 ]
     * Include the current resource alias. On by default.
     */
    'showCurrentAlias' => isset($showCurrentAlias) ? (integer)$showCurrentAlias : 1,

    /**
     * $showRootAlias [ 0 | 1 ]
     * Include the root resource alias. On by default.
     */
    'showRootAlias'    => isset($showRootAlias) ? (integer)$showRootAlias : 1,

    /**
     * $prefix [ string ]
     * You can determine your own prefix.
     */
    'prefix'           => isset($prefix) ? htmlentities($prefix, ENT_QUOTES, $modx->config['modx_charset']) : '',

    /**
     * $attribute [ string ]
     * You can determine your own attributes.
     */
    'attribute'        => isset($attribute) ? htmlentities($attribute, ENT_QUOTES, $modx->config['modx_charset']) : 'class'
);

if (!function_exists(getAliases)) {
    function getAliases($modx, $settings)
    {
        $pageId = $settings['startId'];
        $cnt = 0;
        $output = '';

        if ($settings['number'] !== NULL && is_numeric($settings['number']) {
            // Iterate through parents till we hit root or count up 1000.
            while ($cnt < 1000) {
                // Get the page information.
                $arrPageInfo = $modx->getPageInfo($pageId, 0, "id,parent,alias,hidemenu");

                $pageId = (integer)$arrPageInfo['parent'];
                $cnt = $cnt + 1;

                // If you continue, do not match the number and counted or number is zero.
                if (($cnt !== $settings['number'] || $settings['number'] === 0) && $pageId !== 0) {
                    continue;
                }

                // Check for include conditions.
                if ($settings['respectHidemenu'] !== 1 || (integer)$arrPageInfo['hidemenu'] !== 1) {
                    // Remove the extension.
                    $strAlias = $arrPageInfo['alias'];
                    $dotpos = strrpos($strAlias, '.');
                    $strAlias = is_numeric($dotpos) ? substr($strAlias, 0, $dotpos) : $strAlias;

                    if ($strAlias !== '') {
                        $output = $settings['prefix'] . $strAlias;
                    }
                }

                break;
            }
        } else {
            $arrAlias = array();

            // Iterate through parents till we hit root or count up 1000.
            while ($cnt < 1000) {
                // Get the page information.
                $arrPageInfo = $modx->getPageInfo($pageId, 0, "id,parent,alias,hidemenu");

                $pageId = (integer)$arrPageInfo['parent'];
                $cnt = $cnt + 1;

                 // Check for include conditions.
                if ($settings['respectHidemenu'] !== 1 || (integer)$arrPageInfo['hidemenu'] !== 1) {
                    // Remove the extension.
                    $strAlias = $arrPageInfo['alias'];
                    $dotpos = strrpos($strAlias, '.');
                    $strAlias = is_numeric($dotpos) ? substr($strAlias, 0, $dotpos) : $strAlias;

                    if ($strAlias !== '') {
                        $arrAlias[] = $settings['prefix'] . $strAlias;
                    }
                }

                // If you break, the pageId is zero.
                if ($pageId === 0) {
                    break;
                }
            }

            if ($settings['level'] !== NULL && is_numeric($settings['level'])) {
                // Get the alias of the decision level.
                $output = $arrAlias[count($arrAlias) - $settings['level']];
            } else {
                // Delete an alias for current resource.
                if ($settings['showCurrentAlias'] === 0) {
                    array_shift($arrAlias);
                }

                // Delete an alias for root resource.
                if ($settings['showRootAlias'] === 0) {
                    array_pop($arrAlias);
                }

                // Sort order by Ascending.
                if ($settings['sortOrderAsc'] !== 0) {
                    $arrAlias = array_reverse($arrAlias);
                }

                $output = implode(' ', $arrAlias);
            }
        }

        // Replace the unavailable character.
        preg_replace('/[^A-Za-z0-9\-_]/', '', trim($output));

        // Add an attribute.
        if($settings['attribute'] !== '' && $output !== '') {
            $output = ' ' . $settings['attribute'] . '="' . $output . '"';
        }

        return $output;
    }
}

return getAliases($modx, $settings);